<template>
  <div class="container">
  <el-card>
    <img class="logo" src="@/assets/images/logo.png" alt="" />
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>

      <el-form-item prop="code">
        <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
      </el-form-item>
      <el-form-item prop="checkbox">
        <el-checkbox v-model="form.checkbox">我已阅读并同意「用户协议」和「隐私条款」</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login()">登录</el-button>
      </el-form-item>
    </el-form>
    <!-- 表单 -->
  </el-card>
</div>
</template>

<script>
import auth from '@/utils/token'
export default {
  name: 'LoginPage',
  data () {
    // 自定义校验手机号
    const checkMobile = (rules, value, callback) => {
      if (/^1[3-9]\d{9}$/.test(value)) {
        callback()
      } else {
        callback(new Error('请输入正确格式的手机号'))
      }
    }
    //  callback 失败--->callback(new Error('校验失败的提示'))  成功---->callback()
    const checkAgree = (rules, value, callback) => {
      if (value) {
        callback()
      } else {
        callback(new Error('请勾选用户协议'))
      }
    }
    return {
      // 数据
      form: {
        mobile: '13200000000',
        code: '246810',
        checkbox: false
      },
      // 规则
      rules: {
        // required 必填校验
        // message 错误提示
        // validator 自定义校验函数
        // trigger 触发方式
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { required: true, validator: checkMobile, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { len: 6, message: '验证码6个字符', trigger: 'blur' }
        ],
        checkbox: [
          { required: true, message: '请勾选用户协议', trigger: 'blur' },
          { validator: checkAgree, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    login () {
      // 校验整个表单
      this.$refs.formRef.validate(async (res) => {
        // 如果res为true 则整个表单校验成功
        // $this.$http调用Vue原型上的方法
        if (res) {
          try {
          // 1.发送请求
            const res = await this.$http.post('/v1_0/authorizations', this.form)
            console.log(res)
            // 2.保存token信息
            // localStorage.setItem('geek-client-token', res.data.data.token)
            auth.setToken(res.data.data.token)
            // 3.跳转首页
            this.$router.push('/')
            // 4.1成功提示
            this.$message.success('登陆成功')
          } catch (error) {
            console.log(error)
            // 4.2登录失败提示
            this.$message.error(error.response.data.message || '登录失败')
          }
        }
      })
    }
  }

}
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: url(@/assets/images/login.png);
  .el-card {
    width: 440px;
    height: 380px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    box-shadow: 0 0 50px rgba(0,0,0,0.1);
    .logo {
      width: 200px;
      display: block;
      margin: 0 auto 20px;
    }
  }
}
</style>
